#include "worker.h"
#include <qDebug>
#include <QThread>

Worker::Worker(QObject *parent)
    : QObject{parent}
{

    // 创建工作类实例
    Worker *worker = new Worker();

    // 创建线程
    QThread *thread = new QThread();

    // 将工作类实例移动到线程
    worker->moveToThread(thread);

    // 连接线程的启动和停止信号到槽函数
    QObject::connect(thread, &QThread::started, worker, &Worker::doWork);
    QObject::connect(worker, &Worker::workFinished, thread, &QThread::quit);
    QObject::connect(worker, &Worker::workFinished, worker, &Worker::deleteLater);
    QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);

    // 启动线程
    thread->start();
}

void Worker::doWork()
{
    qDebug() << "Work is done in thread:" << QThread::currentThreadId();
    // 执行耗时任务...
    emit workFinished(); // 任务完成后发出信号
}
